package com.ruoyi.web.controller.workSafetyStandardization.major;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.pengjie.SnowflakeIdUtils;
import com.ruoyi.comprehensive.domain.Drawing;
import com.ruoyi.workSafetyStandardization.Vo.major.majorVo;
import com.ruoyi.workSafetyStandardization.domain.MajorConfig;
import com.ruoyi.workSafetyStandardization.service.IMajorConfigService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 专业配置Controller
 *
 * @author pengjie
 * @date 2023-12-14
 */
@RestController
@RequestMapping("/secure/securespeciality")
public class MajorConfigController extends BaseController
{
    @Autowired
    private IMajorConfigService majorConfigService;

    /**
     * 查询专业配置列表
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:major:list')")
    @GetMapping("/list")
    public TableDataInfo list(MajorConfig majorConfig,@Param("pageSize") String pageSize,@Param("pageNum") String pageNum)
    {
//        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
//        System.out.println(pageNum+" "+pageSize);
//        List<MajorConfig> list = majorConfigService.selectMajorConfigList(majorConfig);
//        PageInfo<MajorConfig> pageInfo=new PageInfo<>(list,Integer.parseInt(pageSize));
//        return pageInfo;
        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        List<MajorConfig> list = majorConfigService.selectMajorConfigList(majorConfig);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:major:list')")
    @GetMapping("/queryAll")
    public AjaxResult list(MajorConfig majorConfig)
    {
        return success(majorConfigService.selectMajorConfigListTree(majorConfig));
    }
    /**
     * 导出专业配置列表
     */
//    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:major:export')")
    @Log(title = "专业配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MajorConfig majorConfig)
    {
        List<MajorConfig> list = majorConfigService.selectMajorConfigList(majorConfig);
        ExcelUtil<MajorConfig> util = new ExcelUtil<MajorConfig>(MajorConfig.class);
        util.exportExcel(response, list, "专业配置数据");
    }

    /**
     * 预览专业配置详细信息
     */
//    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:major:preview')")
    @Anonymous
    @RequestMapping( "/preview")
    public AjaxResult getInfo(@Param("majorId") String majorId)
    {

        return success(majorConfigService.selectMajorConfigByMajorId(majorId));
    }

    /**
     * 查询专业关键字
     */
//    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:major:query')")
    @GetMapping( "/query")
    public TableDataInfo query(@Param("Name") String Name,@Param("pageSize") String pageSize,@Param("pageNum") String pageNum)
    {
        System.out.println(Name+" "+pageNum+" "+pageSize);
        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        List<MajorConfig> list =majorConfigService.selectMajorConfigBymajorName(Name);
        return getDataTable(list);
//        return success(majorConfigService.selectMajorConfigBymajorName(majorName));
    }

    /**
     * 新增专业配置
     */
//    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:major:add')")
    @Log(title = "专业配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody majorVo majorConfigvo)
    {
        MajorConfig majorConfig=new MajorConfig();
        BeanUtils.copyProperties(majorConfigvo,majorConfig);
        if ("".equals(majorConfigvo.getEnable())){
            majorConfig.setEnable(0L);
        }else {
            if (majorConfigvo.getEnable()){
                majorConfig.setEnable(1L);
            }else {
                majorConfig.setEnable(0L);
            }
        }
        majorConfig.setMajorId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
        return toAjax(majorConfigService.insertMajorConfig(majorConfig));
    }

    /**
     * 修改专业配置
     */
//    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:major:edit')")
    @Log(title = "专业配置", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult edit( @RequestBody majorVo majorConfigvo)
    {
        MajorConfig majorConfig=new MajorConfig();
        BeanUtils.copyProperties(majorConfigvo,majorConfig);
        if ("".equals(majorConfigvo.getEnable()) || majorConfigvo.getEnable()){
            majorConfig.setEnable(1L);
        }else {
            majorConfig.setEnable(0L);
        }
        if ("".equals(majorConfigvo.getChild()) || majorConfigvo.getChild()){
            majorConfig.setChild(1L);
        }else {
            majorConfig.setChild(0L);
        }
        return toAjax(majorConfigService.updateMajorConfig(majorConfig));
    }

    /**
     * 删除专业配置
     */
//    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:major:remove')")
    @Log(title = "专业配置", businessType = BusinessType.DELETE)
	@GetMapping("/delete")
    public AjaxResult remove(@Param("majorId") String majorId)
    {
        String[] majorIds={majorId};
        System.out.println(majorId);
        return toAjax(majorConfigService.deleteMajorConfigByMajorIds(majorIds));
    }

    /**
     * 通过父专业查询子专业配置
     */
//    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:major:remove')")
    @Log(title = "专业配置", businessType = BusinessType.DELETE)
    @GetMapping("/query/children")
    public TableDataInfo remove(@Param("parentId") String parentId,@Param("pageSize") String pageSize,@Param("pageNum") String pageNum)
    {
        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        List<MajorConfig> list =majorConfigService.queryMajorConfigByparentId(parentId);
        return getDataTable(list);
//        return success(majorConfigService.queryMajorConfigByparentId(parentId));
    }
}
